! ******************************************************************************
!  Purpose:
!     delete the left blanks of the input string
!  Input:
!     strings: input strings
!  Output:
!     strings: strings which is deleted left blanks
!  Provided by:
!      Deng Liantang 2004.03.19
! ******************************************************************************
       SUBROUTINE l_trim(strings)
       implicit none 
       character*(*),intent(inout) :: strings

       do while((strings(1:1).EQ.' ') .and. (len_trim(strings).NE.0))
         strings=strings(2:)
       end do

       return
       END SUBROUTINE l_trim

! *********************************************************
! 
!   check if there are any blank space in the time strings
!   except end of the time strings. If there are, replace 
!   them with character zero '0'.
! 
!   created by Deng Liantang, 2003. 7. 1
! ********************************************************* 
      SUBROUTINE replace_space(str_name)
      CHARACTER (len =*), INTENT(inout)   :: str_name 
      
      INTEGER        :: len_str ,i 
      
      len_str = len_trim(str_name) 
      
      do i = 1, len_str 
        if (str_name(i:i) .eq. ' ') str_name(i:i) = '0'
      end do 
      
      END SUBROUTINE replace_space

! *********************************************************
!
!   delete any blank space in the strings except end of the strings.
!
!   created by Deng Liantang, 2003. 11. 5
! *********************************************************
      SUBROUTINE delete_space(str_name)
      CHARACTER (len =*), INTENT(inout)   :: str_name

      INTEGER        :: len_str ,i,count

      len_str = len_trim(str_name)
      i = 1
      do while(i .LE. len_str)
        if (str_name(i:i) .eq. ' ') then 
          str_name(i:len_str-1) = str_name(i+1:len_str)
          len_str = len_str - 1
        endif
        i = i+1
      end do
      str_name(len_str+1:) = ''
      END SUBROUTINE delete_space

! ******************************************************************************
!  Purpose:
!     handle the exceptions
!  Input:
!     program: program name whichi generated the exceptions
!     msg: exception messages 
!     debug: debug level flag
!     ierr: exception status
!  Output:
!  Provided by:
!      Deng Liantang 2004.03.19
! ******************************************************************************
        SUBROUTINE do_err(program,msg,debug,ierr)
          CHARACTER(len=*), INTENT(in)  :: program,msg
          INTEGER         , INTENT(in)  :: debug
          INTEGER         , INTENT(in)  :: ierr
          if (ierr .EQ. 0) then 
            if (debug .GE. 500) write(6,*) TRIM(program),': successfully ',TRIM(msg)
            return
          elseif (ierr .GT. 0) then 
            write(6,*) "[ERROR]",TRIM(program),": could not ",TRIM(msg)
            stop
          else 
            write(6,*) "[WARNNING]",TRIM(program),": ",TRIM(msg)
            return
          endif 
        END SUBROUTINE do_err

! ******************************************************************************
!  Purpose:
!     search a idle unit
!  Input:
!  Output:
!     iunit: the idle unit
!  Provided by:
!      Deng Liantang 2004.03.19
! ******************************************************************************
       SUBROUTINE search_unit(iunit)
       IMPLICIT NONE

       integer,  intent(out)  :: iunit
       logical  :: already_opened

       DO iunit = 10,99
         inquire(iunit,OPENED=already_opened)
         if (.not.already_opened) then
           exit
         endif
       END DO
       if (already_opened)  stop ': No idle device used to create file!'

       END SUBROUTINE search_unit

! ******************************************************************************
!  Purpose:
!      change the string from lowercase into capital 
!  Input: 
!     string: the input string
!  Output:
!     string: the output string
!  Provided by:
!      Deng Liantang 2004.09.16
! ******************************************************************************
       SUBROUTINE upcase(string)
       IMPLICIT NONE
       CHARACTER(len = *), INTENT(inout) :: string

       INTEGER :: length, iascii,i

       length = LEN_TRIM(string)
       do i = 1, length
         iascii = ICHAR(string(i:i))
         if ( (iascii .GT. 96) .AND. (iascii .LT. 123) )  then
           string(i:i) = CHAR(iascii - 32)
         endif
       enddo ! i = 1, length
       END SUBROUTINE upcase

! ******************************************************************************
!  Purpose:
!      change the string from capital into lowercase 
!  Input: 
!     string: the input string
!  Output:
!     string: the output string
!  Provided by:
!      Deng Liantang 2004.09.16
! ******************************************************************************
       SUBROUTINE lowcase(string)
       IMPLICIT NONE
       CHARACTER(len = *), INTENT(inout) :: string

       INTEGER :: length, iascii,i

       length = LEN_TRIM(string)
       do i = 1, length
         iascii = ICHAR(string(i:i))
         if ( (iascii .GT. 63) .AND. (iascii .LT. 91) )  then
           string(i:i) = CHAR(iascii + 32)
         endif
       enddo ! i = 1, length
       END SUBROUTINE lowcase
